package com.gupt.reggie.controller;/*
@author name
@data 2023/4/29-15:51
*/

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gupt.reggie.common.R;
import com.gupt.reggie.entity.ShoppingCart;
import com.gupt.reggie.service.ShoppingCartService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(HttpSession session, @RequestBody ShoppingCart shoppingCart){
        ShoppingCart cartServiceOne = shoppingCartService.add(session,shoppingCart);
        return R.success(cartServiceOne);
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session){
        log.info("查看购物车...");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,session.getAttribute("user"));
        queryWrapper.ge(ShoppingCart::getNumber,1);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);


        //  LambdaQueryWrapper  QueryWrapper 区别，字段填充的灵活性
//        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper();
//        wrapper.eq("user_id",BaseContext.getCurrentId());
//        //添加条件，当数量<=1时，不显示在购物车
//        //其实菜品或套餐有存在数据库，知识数量为0 而已
//        wrapper.ge("number","1");
//        wrapper.orderByAsc("create_time");
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * q清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(HttpSession session){

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,session.getAttribute("user"));
        shoppingCartService.remove(queryWrapper);
        return R.success("删除成功");
    }

    /**
     * 菜品或套餐数量数量 每当点击后则数量减一
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<String> delByDishId(@RequestBody ShoppingCart shoppingCart){
        //判断当前套餐还是菜品数量减一
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        if(shoppingCart.getDishId() != null){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //每当点击后则数量减一
        //查询数据库中菜品或套餐的数量 后减一，赋值给shoppingCart 后再次更新购物车数据
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        //设置数量减一
        Integer number = cartServiceOne.getNumber();
        cartServiceOne.setNumber(number - 1);

        //后再次更新购物车数据
        shoppingCartService.updateById(cartServiceOne);
        return R.success("减少数量成功");
    }
}
